# coding=utf-8
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import *
# 1.
# x =np.linspace(50,150,25)
# y = np.linspace(50,150,25)
# x,y = np.meshgrid(x,y)
# print x[:2]
# z = (x-100)**2/(100*x)/y
# fig = plt.figure(figsize=(7,5))
# ax = fig.gca(projection='3d')
# surf = ax.plot_surface(x,y,z,rstride=2,alpha=0.8,cstride=2,cmap=plt.cm.coolwarm,linewidth=0.5,antialiased=True)
# ax.set_xlabel("x index")
# ax.set_ylabel('y Index')
# ax.set_zlabel('z Index')
# plt.title("Frist Sample Line")
# plt.show()
# 2.
'''
x = np.arange(-12,12)
y = np.arange(-9,9)
x,y = np.meshgrid(x,y)
r = np.sqrt(x**2+y**2)
z = np.cos(r)
fig = plt.figure(figsize=(7,5))
ax = fig.gca(projection='3d')
surf = ax.plot_surface(x,y,z,rstride=2,alpha=0.8,cstride=2,cmap=plt.cm.coolwarm,linewidth=0.5,antialiased=True)
# rstride:行之间的跨度  cstride:列之间的跨度
# rcount:设置间隔个数，默认50个，ccount:列的间隔个数  不能与上面两个参数同时出现
ax.contourf(x,y,z,zdir='z',offset=-2)
#vmax和vmin  颜色的最大值和最小值
ax.set_zlim(-3,3)
ax.set_xlabel("x index")
ax.set_ylabel('y Index')
ax.set_zlabel('z Index')
plt.title("Frist Sample Line")
plt.show()
'''
'''
 # 3.绘制多面体
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
from mpl_toolkits.mplot3d.art3d import Line3DCollection
fig =plt.figure(figsize=(7,5))
ax = Axes3D(fig)
dingdians = [(0, 0, 0), (2, 0, 0), (2, 2, 0), (0, 2, 0), (0, 0, 2), (2, 0, 2), (2, 2, 2), (0, 2, 2)]
faces = [[0, 1, 2, 3], [4, 5, 6, 7], [0, 1, 5, 4], [1, 2, 6, 5], [2, 3, 7, 6], [0, 3, 7, 4]]
poly3d =[[dingdians[dingdian] for dingdian in face]for face in faces]
x,y,z = zip(*dingdians)
ax.scatter(x,y,z)
# 绘制多边形面
ax.add_collection3d(Poly3DCollection(poly3d,facecolors='w',linewidths=1,linestyles=':',alpha=0.4))
# 绘制对变形的边
ax.add_collection3d(Line3DCollection(poly3d,colors='m',linestyles='--',linewidths=0.5))
ax.set_xlabel('x vaule')
ax.set_ylabel('y vaule')
ax.set_zlabel('z vaule')
ax.set_xlim3d(-3,3)
ax.set_ylim3d(-3,3)
ax.set_zlim3d(-3,3)
plt.show()
# 4.三个方向有等高线的3D图
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from mpl_toolkits.mplot3d import axes3d

fig = plt.figure()
ax = fig.gca(projection='3d')
X, Y, Z = axes3d.get_test_data(0.05)
ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.5, color='b')
cset = ax.contour(X, Y, Z, zdir='z', offset=-100, cmap=cm.coolwarm)
cset = ax.contour(X, Y, Z, zdir='x', offset=-40, cmap=cm.coolwarm)
cset = ax.contour(X, Y, Z, zdir='y', offset=40, cmap=cm.coolwarm)

ax.set_xlabel('X')
ax.set_xlim(-40, 40)
ax.set_ylabel('Y')
ax.set_ylim(-40, 40)
ax.set_zlabel('Z')
ax.set_zlim(-100, 100)
plt.show()
'''

# 三维柱状图
'''
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = Axes3D(fig)
x =[0,1,2,3,4,5,6]
for i in x:
    y =[0,1,2,3,4,5,6,7,8,9]
    z = abs(np.random.normal(1,10,10))
    ax.bar(y,z,i, zdir='y',color=['r','g','b','y'])
plt.show()
'''
# fig = plt.figure()
fig = plt.figure()
ax = Axes3D(fig)
ax1 = fig.add_subplot(1,2,1,property='3d')
x = np.linspace(-6*np.pi,6*np.pi,100)
y = np.cos(x)
z = np.sin(x)
ax2 = fig.add_subplot(1,2,2,property='3d')
X = np.arange(-2, 2, 0.1)
Y = np.arange(-2, 2, 0.1)
Z = np.sqrt(x**3+y**2)
ax3 = fig.add_subplot(1,2,3,property='3d')
x0 =np.linspace(50,150,25)
y0 = np.linspace(50,150,25)
x0,y0 = np.meshgrid(x0,y0)
z0 = (x0 - 100) ** 2 / (100 * x0) / y0
plt.show()